package cn.com.nes.site.entity.vo.project;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.common.util.id.IdUtils;
import cn.com.nes.site.entity.bo.DeviceSaveBo;
import cn.com.nes.site.entity.bo.callcenter.CallCenterEquipmentArchivesBo;
import cn.com.nes.site.entity.bo.ope.OpeEquipmentArchivesBo;
import cn.com.nes.site.entity.em.BusinessType;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 保存个人项目接口入参模型（厂商）
 */
@Data
public class PersonalProjectSaveForManufactureVo extends PersonalProjectSaveVo {

    /**
     * 联系人
     */
    @NotBlank(message = "联系人")
    private String contact;

    /**
     * 联系电话
     */
    @NotBlank(message = "联系电话")
    private String contactPhone;

    /**
     * 身份证号
     */
    @NotBlank(message = "身份证号")
    private String idCardNumber;

    /**
     * 行政区划代码
     */
    @NotBlank(message = "行政区划代码")
    private String areaCode;

    /**
     * 项目地址
     */
    @NotBlank(message = "项目地址")
    private String projectAddress;

    /**
     * 经度
     */
    @NotNull(message = "经度")
    private BigDecimal longitude;

    /**
     * 纬度
     */
    @NotNull(message = "纬度")
    private BigDecimal latitude;

    /**
     * 地点ID
     */
    private String ddId;

    /**
     * 设备列表
     */
    @NotEmpty(message = "设备列表")
    private List<DeviceSaveVo> deviceSaveVos;


    private List<DeviceSaveBo> opeDeviceSaveBos;


    /**
     * 转换成长效管护设备档案数据
     * @param callCenterCompanyId 长效管护公司ID
     * @return
     */
    public CallCenterEquipmentArchivesBo toCallCenterEquipmentArchivesBo(String callCenterCompanyId,
            String opeManufactureId) throws Exception {
        CallCenterEquipmentArchivesBo bo = new CallCenterEquipmentArchivesBo();
        bo.setContact(this.getContact());
        bo.setContactPhone(this.getContactPhone());
        bo.setIdCardNumber(this.getIdCardNumber());
        bo.setProjectAddress(this.getProjectAddress());
        bo.setManufactureId(opeManufactureId);
        bo.setAreaCode(this.getAreaCode());
        bo.setLatitude(this.getLatitude());
        bo.setLongitude(this.getLongitude());
        bo.setCallCenterCompanyId(Long.parseLong(callCenterCompanyId));
        bo.setDdId(this.getDdId());
        bo.setDeviceSaveBos(this.getOpeDeviceSaveBos());
        bo.setSource(BusinessType._agent.getKey());

        // 操作人信息
        bo.settingOperateInfo(this);
        return bo;
    }

    /**
     * 转换为运营平台设备档案数据
     * @param opeCompanyId 运营平台公司ID
     * @return
     */
    public OpeEquipmentArchivesBo toOpeEquipmentArchivesBo(String opeCompanyId) throws Exception {
        OpeEquipmentArchivesBo bo = new OpeEquipmentArchivesBo();
        bo.setContact(this.getContact());
        bo.setContactPhone(this.getContactPhone());
        bo.setIdCardNumber(this.getIdCardNumber());
        bo.setProjectAddress(this.getProjectAddress());
        bo.setAreaCode(this.getAreaCode());
        bo.setLatitude(this.getLatitude());
        bo.setLongitude(this.getLongitude());
        bo.setManufactureId(opeCompanyId);
        bo.setDdId(this.getDdId());
        this.setOpeDeviceSaveBos(toDeviceSaveBos());
        bo.setDeviceSaveBos(this.getOpeDeviceSaveBos());

        // 操作人信息
        bo.settingOperateInfo(this);
        return bo;
    }

    /**
     * 转换为设备保存业务模型
     * @return
     */
    private List<DeviceSaveBo> toDeviceSaveBos() throws Exception {
        List<DeviceSaveBo> list = new ArrayList<>();
        for (DeviceSaveVo vo : this.getDeviceSaveVos()) {
            DeviceSaveBo bo = new DeviceSaveBo();
            if (StrUtil.isEmpty(vo.getSbId())) {
                // 没有设备ID，表示新增，生成设备ID
//                bo.setSbId(DBUtils.getDjbh(this.getOperateUserCompanyId(), "SB_"));
                bo.setSbId("SB_" + IdUtils.getIncreaseIdByNanoTime());
                bo.setDeleted(DeleteEnum._0.getKey());
                bo.setGmtCreate(new Date());
                bo.setCreateUserName(this.getOperateUserName());
                bo.setCreateUserId(this.getOperateUserId());
            } else {
                // 不为空，表示修改
                bo.setSbId(vo.getSbId());
                if (ObjectUtil.isNotNull(vo.getDeleted())) {
                    bo.setDeleted(vo.getDeleted());
                }
                bo.setGmtModified(new Date());
                bo.setModifiedUserName(this.getOperateUserName());
                bo.setModifiedUserId(this.getOperateUserId());
            }
            bo.setDdId(vo.getDdId());
            bo.setManufacturerId(vo.getManufactureId());
            bo.setDeviceTypeId(vo.getDeviceTypeId());
            bo.setDeviceModelId(vo.getDeviceModelId());
            bo.setDeviceSerialNo(vo.getDeviceSerialNo());
            bo.setDtuId(vo.getDtuId());
            bo.setWarrantyDate(vo.getWarrantyDate());
            bo.setImages(vo.getImages());
            bo.setRemark(vo.getRemark());
            list.add(bo);
        }
        return list;
    }

}